# -*- coding: utf-8 -*-
from django.utils.translation import ugettext as _
from django.http import HttpResponse
from django.db import models
from django.views.decorators.cache import cache_page
from blog.models import Post, Page
from catalog.models import Suggestion, Dataset, License
from util.breadcrumb import breadcrumb_from_objects
from util.comments_extra import post_comment

from django.shortcuts import render_to_response, get_object_or_404
from django.contrib.comments.views.comments import CommentPostBadRequest 
from django.contrib.comments.views.utils import next_redirect
from django.contrib import comments
from django.contrib.comments.models import Comment
from django.contrib.comments import signals
from time import strftime
import datetime
from itertools import chain

@cache_page(60 * 60)
def postfeed(request):
	post_list = Post.objects.order_by('-pub_date')[:10]
	last_updated = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S+01:00")
	if len(post_list) > 0:
		last_updated = post_list[0].pub_date.strftime("%Y-%m-%dT%H:%M:%S+01:00")
	return render_to_response('postfeed.html', locals(), mimetype='application/atom+xml')

@cache_page(60 * 60)
def commentsfeed(request):
	comment_list = Comment.objects.filter(is_public=True).order_by('-submit_date')[:10]
	last_updated = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S+01:00")
	if len(comment_list) > 0:
		last_updated = comment_list[0].submit_date.strftime("%Y-%m-%dT%H:%M:%S+01:00")
	return render_to_response('commentfeed.html', locals(), mimetype='application/atom+xml')




@cache_page(10 * 60)
def index(request):
	post_list = Post.objects.all()

	comment_list = Comment.objects.filter(is_public=True, is_removed=False).order_by('-submit_date')[:20]
	recent_list = sorted(chain(comment_list), key=lambda item: item.submit_date)
	recent_list.reverse()
	recent_list = recent_list[:10]
	
	recent_suggestions = Suggestion.objects.all().order_by("-id")[:10]
	datasource_count = Dataset.objects.all().count()
	datasource_free_count = Dataset.objects.filter(license__is_free=True).count() 

	datasource_free_percent = 0
	if datasource_count > 0:
		datasource_free_percent = int(100.0 * datasource_free_count / datasource_count)

	page_title = _("Welcome") #Site front page heading
	current = "blog"
	return render_to_response('index.html', locals())


@cache_page(10 * 60)
def blogpost(request, year, slug):
	post = get_object_or_404(Post, slug=slug)
	page_title = post.title
	breadcrumb = breadcrumb_from_objects([{"title": "News", "url": "/blog/"}])

	current = "blog"
	if request.method == "POST":
		return post_comment(request, None, post, locals(), "post.html")

	#default form
	form = comments.get_form()(post, None)
	return render_to_response('post.html', locals())


def page_index(request):
	page = get_object_or_404(Page, slug="index")
	page_title = page.title
	breadcrumb = breadcrumb_from_objects([page])
	base_template = "base.html"
	if page.use_full_width:
		base_template = "base_full_width.html"
	return render_to_response('page.html', locals())


def page(request, slug):
	page = get_object_or_404(Page, slug=slug)
	page_title = page.title

	base_template = "base.html"
	if page.use_full_width:
		base_template = "base_full_width.html"

	current = ""

	return render_to_response('page.html', locals())

